<html>
<head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head>
<body>
<?
function request_cache($url, $dest_file, $timeout=43200) {
  if(!file_exists($dest_file) || filemtime($dest_file) < (time()-$timeout)) {
    $stream = fopen($url,'r');
    $tmpf = tempnam('/tmp','YWS');
    file_put_contents($tmpf, $stream);
    fclose($stream);
    rename($tmpf, $dest_file);
  }
}

function yahoo_geo($location) {
  $q = 'http://api.local.yahoo.com/MapsService/V1/geocode';
  $q .= '?appid=YD-9G7bey8_JXxQP6rxl.fBFGgCdNjoDMACQA--&location='.$location;
  
  //$stream = fopen($q,'r');
  $tmp = file_get_contents($q);
  //fclose($stream);

  libxml_use_internal_errors(true);
  $xml = simplexml_load_string($tmp); 

  $ret['precision'] = (string)$xml->Result['precision'];
  foreach($xml->Result->children() as $key=>$val) {
    if(strlen($val)) $ret[(string)$key] =  (string)$val;
  } 
  return $ret;
}

function geonames_geo($call) {
  $q = 'http://ws.geonames.org/search?operator=or&featureCode=PPL&'.$call;
  //$stream = fopen($q,'r');
  $tmp = file_get_contents($q);
  //fclose($stream);
  
  libxml_use_internal_errors(true);
  $xml = simplexml_load_string($tmp); 
  
  if(intval($xml->totalResultsCount) >= 1){
    $ret = $xml->geoname[0]->geonameId[0];
  } 
  return $ret;
}

function dbpedia_uri($call) {
  $q = 'http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=&MaxHits=1&QueryString='.$call;
  if($tmp = file_get_contents($q)) {
		
	  libxml_use_internal_errors(true);
	  $xml = simplexml_load_string($tmp); 
	 
	  $ret = $xml->Result[0]->URI;  
	  return $ret;
  }
  else{
	return '';
  }
}

set_time_limit(1200);
mysql_connect("localhost", "root", "w8woord-ivaldi") or die(mysql_error());
mysql_select_db("publicationsv2") or die(mysql_error());

/*$query = mysql_query("SELECT address,entry_id FROM inproceedings WHERE address != ''");

$i = 0;
$geonamesid = '';
while($res = mysql_fetch_array($query)){

	$location = $res['address'];
	$geonamesid = geonames_geo('q='.urlencode($location));
	
	if($geonamesid == ''){
		$yahoo = yahoo_geo(urlencode($location));
		$geonamescall = 'name='.$yahoo['City'].'&country='.$yahoo['Country'];
		$geonamesid = geonames_geo($geonamescall);
	}	
	
	if($geonamesid != ''){
		$url = 'http://sws.geonames.org/'.$geonamesid;
		$q = "INSERT INTO address_dn (id, location, geonames, entry_id) VALUES ('', '".$res['address']."', '".$url."/', '".$res['entry_id']."')";
		mysql_query($q);
	}	
	
	$genoamesid = '';
	
}*/
$tables = array('book', 'booklet', 'inbook', 'incollection', 'inproceedings','proceedings');
foreach($tables as $table) {

	$query = mysql_query("SELECT publisher,entry_id FROM ".$table." WHERE publisher != ''");

	while($res = mysql_fetch_array($query)){

		$publisher = $res['publisher'];
		
		if(preg_match("/ieee/i",$publisher)){
			$dbpedia_uri = 'http://dbpedia.org/resource/IEEE_Computer_Society_Press';
		}
		elseif(preg_match("/springer/i",$publisher)){
			$dbpedia_uri = 'http://dbpedia.org/resource/Springer_Science%2BBusiness_Media';
		}
		elseif(preg_match("/acm/i",$publisher)){
			$dbpedia_uri = 'http://dbpedia.org/resource/Association_for_Computing_Machinery';
		}	
		else{
			$dbpedia_uri = dbpedia_uri(urlencode($publisher));
		}
		
		echo $publisher."\t\t\t\t".$dbpedia_uri.'<br/>';
		
		if($dbpedia_uri != ''){
			$url = $dbpedia_uri;
			$q = "INSERT INTO publisher_dn (id, publisher, dbpedia, entry_id) VALUES ('', '".$publisher."', '".$url."', '".$res['entry_id']."')";
			mysql_query($q);
		}
			
	}
}


?>
<h1>Done succesfully!</h1>
</body>
</html>